//
//  SPManageAddController.swift
//  SparklingPets
//
//  Created by iOS-Czz on 2023/10/26.
//

import UIKit
import WMZDialog
class SPManageAddController: SPBaseController {
    @IBOutlet var timeTF: UITextField!
    @IBOutlet var ratorTF: UITextField!
    @IBOutlet var manageTF: UITextField!
    @IBOutlet var dateTF: UITextField!
    @IBOutlet var nameTF: UITextField!
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }
   
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.wx_navigationBar.isHidden = true
    }
    @IBAction func clickBackAction(_ sender: Any) {
        self.navigationController?.popViewController(animated: true)
    }
    @IBAction func clickSaveAction() {
        self.view.endEditing(true)
        if self.dateTF.text!.isEmpty {
            UIView.showTipsView("Please select date")
            return
        }
        if self.nameTF.text!.isEmpty {
            UIView.showTipsView("Please enter pet name")
            return
        }
        if self.manageTF.text!.isEmpty {
            UIView.showTipsView("Please enter manage content")
            return
        }
        if self.ratorTF.text!.isEmpty {
            UIView.showTipsView("Please enter operator")
            return
        }
        if self.timeTF.text!.isEmpty {
            UIView.showTipsView("Please select plan time")
            return
        }
        let model = SPManageModel()
        model.time = self.timeTF.text!
        model.name = self.nameTF.text!
        model.isCompleted = false
        model.manage = self.manageTF.text!
        model.rator = self.ratorTF.text!
        model.date = self.dateTF.text!
        model.isDefault = false
        model.saveToDB()
        UIView.showResultView("Manage addition succeed", true)
        let helper = SPManageModel.getUsingLKDBHelper()
        helper.delete(with: SPManageModel.self, where: "isDefault = true")
        let delay = 1.5 // 2秒
        let dispatchTime = DispatchTime.now() + delay
        DispatchQueue.main.asyncAfter(deadline: dispatchTime) {
            self.navigationController?.popToRootViewController(animated: true)
        }
    }
    @IBAction func clickSelectPlanTimeAction(_ sender: Any) {
        self.view.endEditing(true)
        let dialog = Dialog()
        dialog.wDateTimeTypeSet()("HH:mm")
        .wDefaultDateSet()(Date())
        .wCancelTitleSet()("Cancel")
        .wOKTitleSet()("Ok")
        .wEventOKFinishSet()({(anyId:Any?,otherData:Any?)in
            if let formattedTime = Date.formatTime(inputTime: otherData as! String) {
                self.timeTF.text = formattedTime
            } else {
                print("Invalid time format")
            }
        })
        .wTypeSet()(DialogTypeDatePicker);
        _ = dialog.wStart()
    }
    @IBAction func clickSelectDateAction(_ sender: Any) {
        self.view.endEditing(true)
        let dialog = Dialog()
        dialog.wDateTimeTypeSet()("yyyy-MM-dd")
        .wDefaultDateSet()(Date())
        .wCancelTitleSet()("Cancel")
        .wOKTitleSet()("Ok")
        .wEventOKFinishSet()({(anyId:Any?,otherData:Any?)in
            self.dateTF.text = otherData as? String
        })
        .wTypeSet()(DialogTypeDatePicker);
        _ = dialog.wStart()
    }
    
   
}
